var a = !1, o = !1, s = 2;

Page({
    data: {
        status: 0,
        order_list: [],
        show_no_data_tip: !1,
        hide: 1,
        qrcode: "",
        is_clerk:0,
      now_order: []
    },
    onLoad: function(t) {

        getApp().page.onLoad(this, t);
        var r = this;
        a = !1, o = !1, s = 2, r.loadOrderList(t.status || -1), getCurrentPages().length < 2 && r.setData({
            show_index: !0
        });
    },
    loadOrderList: function(a) {
        void 0 == a && (a = -1);
        var o = this;
        o.setData({
            status: a
        }), wx.showLoading({
            title: "正在加载",
            mask: !0
        }), getApp().request({
            url: getApp().api.share.get_team_order,
            data: {
                status: o.data.status,
            },
            success: function(t) {
                0 == t.code && (o.setData({
                    order_list: t.data.list,
                    pay_type_list: t.data.pay_type_list,
                  now_order: t.data.now_order,
                }), wx.getStorageSync("item") && wx.removeStorageSync("item")), o.setData({
                    show_no_data_tip: 0 == o.data.order_list.length
                });
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    onReachBottom: function() {
        var r = this;
        o || a || (o = !0, getApp().request({
            url: getApp().api.share.get_team_order,
            data: {
                status: r.data.status,
                page: s
            },
            success: function(t) {
                if (0 == t.code) {
                    var e = r.data.order_list.concat(t.data.list);
                    r.setData({
                        order_list: e,
                        pay_type_list: t.data.pay_type_list,
                      now_order: t.data.now_order,
                    }), 0 == t.data.list.length && (a = !0);
                }
                s++;
            },
            complete: function() {
                o = !1;
            }
        }));
    },
    orderPay_1: function(t) {
        var e = this, a = e.data.pay_type_list;
        1 == a.length ? (wx.showLoading({
            title: "正在提交",
            mask: !0
        }), 0 == a[0].payment && e.WechatPay(t), 3 == a[0].payment && e.BalancePay(t)) : wx.showModal({
            title: "提示",
            content: "选择支付方式",
            cancelText: "余额支付",
            confirmText: "线上支付",
            success: function(a) {
                wx.showLoading({
                    title: "正在提交",
                    mask: !0
                }), a.confirm ? e.WechatPay(t) : a.cancel && e.BalancePay(t);
            }
        });
    },
    WechatPay: function(a) {
        e.request({
            url: t.order.pay_data,
            data: {
                order_id: a.currentTarget.dataset.id,
                pay_type: "WECHAT_PAY"
            },
            complete: function() {
                wx.hideLoading();
            },
            success: function(t) {
                0 == t.code && wx.requestPayment({
                    _res: t,
                    timeStamp: t.data.timeStamp,
                    nonceStr: t.data.nonceStr,
                    package: t.data.package,
                    signType: t.data.signType,
                    paySign: t.data.paySign,
                    success: function(t) {},
                    fail: function(t) {},
                    complete: function(t) {
                        "requestPayment:fail" != t.errMsg && "requestPayment:fail cancel" != t.errMsg ? wx.redirectTo({
                            url: "/user/order/order?status=1"
                        }) : wx.showModal({
                            title: "提示",
                            content: "订单尚未支付",
                            showCancel: !1,
                            confirmText: "确认",
                            success: function(t) {
                                t.confirm && wx.redirectTo({
                                    url: "/user/order/order?status=0"
                                });
                            }
                        });
                    }
                }), 1 == t.code && wx.showToast({
                    title: t.msg,
                    image: "/images/icon-warning.png"
                });
            }
        });
    },
    BalancePay: function(a) {
        e.request({
            url: t.order.pay_data,
            data: {
                order_id: a.currentTarget.dataset.id,
                pay_type: "BALANCE_PAY"
            },
            complete: function() {
                wx.hideLoading();
            },
            success: function(t) {
                0 == t.code && wx.redirectTo({
                    url: "/user/order/order?status=1"
                }), 1 == t.code && wx.showModal({
                    title: "提示",
                    content: t.msg,
                    showCancel: !1
                });
            }
        });
    },
    orderRevoke: function(a) {
        var o = this;
        wx.showModal({
            title: "提示",
            content: "是否取消该订单？",
            cancelText: "否",
            confirmText: "是",
            success: function(s) {
                if (s.cancel) return !0;
                s.confirm && (wx.showLoading({
                    title: "操作中"
                }), e.request({
                    url: t.order.revoke,
                    data: {
                        order_id: a.currentTarget.dataset.id
                    },
                    success: function(t) {
                        wx.hideLoading(), wx.showModal({
                            title: "提示",
                            content: t.msg,
                            showCancel: !1,
                            success: function(t) {
                                t.confirm && o.loadOrderList(o.data.status);
                            }
                        });
                    }
                }));
            }
        });
    },
    orderConfirm: function(a) {
        var o = this;
        wx.showModal({
            title: "提示",
            content: "是否确认已收到货？",
            cancelText: "否",
            confirmText: "是",
            success: function(s) {
                if (s.cancel) return !0;
                s.confirm && (wx.showLoading({
                    title: "操作中"
                }), e.request({
                    url: t.order.confirm,
                    data: {
                        order_id: a.currentTarget.dataset.id
                    },
                    success: function(t) {
                        wx.hideLoading(), wx.showToast({
                            title: t.msg
                        }), 0 == t.code && o.loadOrderList(3);
                    }
                }));
            }
        });
    },
    orderQrcode: function(a) {
        var o = this, s = o.data.order_list, r = a.target.dataset.index;
        wx.showLoading({
            title: "正在加载",
            mask: !0
        }), o.data.order_list[r].offline_qrcode ? (o.setData({
            hide: 0,
            qrcode: o.data.order_list[r].offline_qrcode
        }), wx.hideLoading()) : e.request({
            url: t.order.get_qrcode,
            data: {
                order_no: s[r].order_no
            },
            success: function(t) {
                0 == t.code ? o.setData({
                    hide: 0,
                    qrcode: t.data.url
                }) : wx.showModal({
                    title: "提示",
                    content: t.msg
                });
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    hide: function(t) {
        this.setData({
            hide: 1
        });
    },
    onShow: function() {
        getApp().page.onShow(this);
    },
    // 取货通知
    tpl_order:function(){
      var o = this, s = o.data.order_list;
      wx.showLoading({
        title: "正在加载",
        mask: !0
      })
      var ss =[];
      for(var i =0 ;i<s.length; i++){
        ss[i] = s[i].order_id;
      }

      getApp().request({
        url: getApp().api.commander.shop_order_tpl,
        method: 'POST',
        data: {
          order: JSON.stringify(ss)
        },
        success: function (t) {
          wx.showModal({
            title: "提示",
            content: t.msg,
            showCancel: !1,
            confirmText: "返回",
            success: function (t) {
              
            }
          });
        },
        complete: function () {
          wx.hideLoading();
        }
      });
    },
    getHexiao: function (t) {
      var e = this, a = t.currentTarget.dataset.hexiao, i = t.currentTarget.dataset.index;
      e.data.order_list[i].hexiao = a;
      e.setData({
        order_list: e.data.order_list
      })
    },
    is_clerk:function(t){
      var that = this;
        this.setData({
          is_clerk: !that.data.is_clerk
        })
    },
  clerkUser: function (tt) {
    var r = this;
    var order_list = {};
    for (var i = 0; i < r.data.order_list.length; i++) {
      if (r.data.order_list[i].hexiao == 1) {
        order_list[i] = r.data.order_list[i].order_id;
      }
    }
    wx.showModal({
      title: "提示",
      content: "是否确认核销？",
      success: function (tt) {
        tt.confirm ? (wx.showLoading({
          title: "正在核销"
        }), getApp().request({
          url: getApp().api.order.clerk,
          data: {
            order_id_arr: order_list
          },
          success: function (e) {
            0 == e.code ? wx.showModal({
              title: "提示",
              showCancel: !1,
              content: e.msg,
              confirmText: "确认",
              success: function (e) {
                e.confirm && wx.redirectTo({
                  url: "/offline/commander/index"
                });
              }
            }) : wx.showModal({
              title: "警告！",
              showCancel: !1,
              content: e.msg,
              confirmText: "确认",
              success: function (e) {
                e.confirm && wx.switchTab({
                  url: "/pages/index/index"
                });
              }
            });
          },
          complete: function () {
            wx.hideLoading();
          }
        })) : t.cancel;
      }
    });
  },
  // 一键复制事件
  copyBtn: function (e) {

    var obf_data = { i_1: '1️⃣', i_2: '2️⃣', i_3: '3️⃣', i_4: '4️⃣', i_5: '5️⃣', i_6: '6️⃣', i_7: '7️⃣', i_8: '8️⃣', i_9: '9️⃣', i_0: '0️⃣'} ;
    var order_list = this.data.now_order;
    if (!order_list || order_list.length < 1) {
      wx.showToast({
        icon:"none",
        title: '今天还没有订单',
      });
      return false;
    }
    var store_name = wx.getStorageSync('store_name')
    var data = " 🌹 " + store_name + '今日团品订单' + " 🌹 " + ' \n';
    for (var i = 0; i < order_list.length; i++) {

      // 商品名称 
      var goods_info = '';
      for (var y = 0; y < order_list[i].goods_list.length; y++) {
        goods_info += "💵" + order_list[i].goods_list[y].goods_name + "*" + order_list[i].goods_list[y].num + '\n';
      }

      var h = i + 1;
      if (h < 100) {
        var ten = Math.floor(h / 10);
        var num = h % 10;
        data += obf_data['i_' + ten] + ' ' + obf_data['i_' + num] + order_list[i].user_name + '\n' + goods_info;
      }
    }

    var that = this;
    wx.setClipboardData({
      //准备复制的数据
      data: data,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
        });
      }
    });
  }
});